import {authenticate} from './authenticate.js'; //引入鉴权方法
import CONSTANTS from './constants.js';
export const request = (options) => {
	return new Promise((resolve, reject) => { //promise实现异步调用
		let auth = authenticate(CONSTANTS.BASEURL + options.url);
		uni.request({
			url: CONSTANTS.BASEURL + options.url, //接口地址：前缀+方法中传入的地址
			method: options.method || 'GET', //请求方法：传入的方法或者默认是“GET”
			data: options.data || {}, //传递参数：传入的参数或者默认传递空集合
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'X-Caller-Id': CONSTANTS.KUMIAPPID,
				'X-Caller-Sign': auth.sign,
				'X-Caller-Timestamp': auth.timestamp
			},
			success: (res) => {
				console.log(res);
				if (res.data.code === '1') {
				return resolve(res)
				} else {
				return reject(res)
				}
			},
			fail: (err) => {
				console.log(err)
				return reject(err)
			}
		})
	})
}